package main

import (
	"fmt"
	"strings"
)

/*
你有50枚金币，需要分配给以下几个人：Matthew,Sarah,Augustus,Heidi,Emilie,Peter,Giana,Adriano,Aaron,Elizabeth。
分配规则如下：
a. 名字中每包含1个'e'或'E'分1枚金币
b. 名字中每包含1个'i'或'I'分2枚金币
c. 名字中每包含1个'o'或'O'分3枚金币
d: 名字中每包含1个'u'或'U'分4枚金币
写一个程序，计算每个用户分到多少金币，以及最后剩余多少金币？
程序结构如下，请实现 ‘dispatchCoin’ 函数
*/
var (
	coins = 50
	users = []string{
		"Matthew", "Sarah", "Augustus", "Heidi", "Emilie", "Peter", "Giana", "Adriano", "Aaron", "Elizabeth",
	}
	distribution = make(map[string]int, len(users))
)

func main() {
	left := dispatchCoin()
	fmt.Println("剩下：", left)
}

//分配金币
func dispatchCoin() int {
	var res = coins
	for i := 0; i < len(users); i++ {
		//fmt.Println(users[i])
		res -= getpatchCoin(users[i])
	}
	return res
}

//获取每个的出现次数
func getContainsNum(s string) map[string]int {
	var arr = []rune(s)
	var sarr = make([]string, len(arr))
	for i := 0; i < len(arr); i++ {
		sarr[i] = string(arr[i])
	}
	var maps = make(map[string]int)
	for i := 0; i < len(arr); i++ {
		maps[sarr[i]]++
	}
	return maps
}

//获取会获取到的金币数
func getpatchCoin(str string) int {
	var res = 0
	maps := getContainsNum(str)
	for key, val := range maps {
		for i := 0; i < val; i++ {
			switch {
			case strings.Contains(key, "e") || strings.Contains(key, "E"):
				//fmt.Println(key)
				res += 1
			case strings.Contains(key, "i") || strings.Contains(key, "I"):
				//fmt.Println(key)
				res += 2
			case strings.Contains(key, "o") || strings.Contains(key, "O"):
				//fmt.Println(key)
				res += 3
			case strings.Contains(key, "u") || strings.Contains(key, "U"):
				//fmt.Println(key)
				res += 4
			}
			//fmt.Print(key)
		}
	}
	return res
}
